<template>
	<z-paging ref="paging" v-model="indexList" @query="queryList" hide-no-more-inside safe-area-inset-bottom
		use-safe-area-placeholder :refresher-enabled="false">
		<template slot="top">
			<cl-navbar autoBack placeholder :title="$t('astral.review.totalEvaluation')"></cl-navbar>
		</template>
		
		
		<view class="commentView">
			<view class="commentView__content">
				<view class="commentView__content__item" v-for="(item,index) in indexList">
					<view class="commentView__content__item__top" v-if="item.user_info">
						<cl-avatar :src="item.user_info.avatar_url" size="70rpx"></cl-avatar>
						<view class="commentView__content__item__top__right">
							<text class="commentView__content__item__top__right__name">{{maskString(item.user_info.nick_name)}}</text>
							<view style="display: flex;align-items: center;">
								<u-rate :value="item.rating" readonly activeColor="#F7797D" size="12"></u-rate>
								<span style="margin-left: 20rpx;font-size: 20rpx;color: #595A66;">{{item.attr_name}}</span>
							</view>
						</view>
					</view>
					<view class="commentView__content__item__details">
						{{item.comments}}
					</view>
					<view class="commentView__content__item__medium">
						<u-album :urls="getAlbuma(item)" singleSize="210" multipleSize="210rpx" space="10rpx"></u-album>
					</view>
				</view>
			</view>
		</view>
	</z-paging>
</template>

<script>
	import mediaCache from '@/common/mediacache/mediaCache.js'; // 适配你的实际路径
	export default {
		name: 'astralreview',
		data() {
			return {
				indexList: [],
				product_id:0,
			}
		},
		onLoad() {
			let {product_id} = this.$Route.query
			this.product_id = product_id
		},
		computed: {
			getAlbuma(){
				return (item)=>{
					return item.stars_product_comment_images && item.stars_product_comment_images.map(item => {
						return {
							src:item.thumb_url,
							videoSrc:item.image_url,
							image_type:item.image_type
						}
					}) || []
				}
			}
		},
		methods: {
			maskString(str) {
			    if (str.length === 2) {
			        // 如果字符串长度为2，将第二位替换为 "*"
			        return str[0] + '*';
			    } else if (str.length > 2) {
			        // 如果字符串长度大于2，保留第一位和最后一位，其余用 "*" 替换
			        let firstChar = str.charAt(0);
			        let lastChar = str.charAt(str.length - 1);
			        let middleStars = '*'.repeat(str.length - 2);
			        return firstChar + middleStars + lastChar;
			    } else {
			        // 如果字符串长度小于等于1，直接返回原字符串
			        return str;
			    }
			},
			async queryList(pageNo, pageSize) {
				try {
					let res = await this.$api.common.getStarsProductParallelCommentList({
						page: pageNo,
						limit: pageSize,
						product_id: this.product_id
					})
					if (res) {
						this.$refs.paging.complete(res.data.list)
						setTimeout(async()=>{
							for (let index in this.indexList) {
								let item = this.indexList[index]
								for (let index_two in item.stars_product_comment_images) {
									let item_two = item.stars_product_comment_images[index_two]
									try {
										const cachedMedia = await mediaCache.loadMedia(item_two.thumb_url, 'stars','image');
										this.indexList[index].stars_product_comment_images[index_two].thumb_url = cachedMedia;
									} catch (error) {
										this.indexList[index].stars_product_comment_images[index_two].thumb_url = '/static/logo.png'
									}
								}
							}
						},100)
					} else {
						this.$refs.paging.complete(false)
					}
				} catch (e) {
					this.$refs.paging.complete(false)
				}
			},
		},
	};
</script>

<style scoped lang="scss">
	.commentView{
		margin: 0 30rpx 0;
		// background: #ffffff;
		border-radius: 10rpx;
		padding: 0 20rpx 20rpx;
		&__top{
			display: flex;
			align-items: center;
			justify-content: space-between;
			&__titie{
				font-size: 32rpx;
				color: #FFFFFF;
			}
			&__good{
				font-size: 24rpx;
				color: #595A66;
			}
		}
		&__tags {
			display: flex;
			gap: 20rpx;
			flex-wrap: wrap;
			margin-top: 20rpx;
			&__item {
				padding: 0 20rpx;
				background: #ededed;
				border-radius: 10rpx;
				height: 46rpx;
				line-height: 46rpx;
				font-size: 24rpx;
				color: #595A66;
			}
		}
		&__content {
			display: flex;
			flex-direction: column;
			width: 100%;
			&__item {
				margin-top: 40rpx;
				&__top{
					display: flex;
					align-items: center;
					&__right{
						margin-left: 10rpx;
						display: flex;
						flex-direction: column;
						&__name{
							font-size: 24rpx;
							color: #595A66;
							margin-bottom: 5rpx;
							margin-left: 5rpx;
						}
					}
				}
				&__details{
					margin: 20rpx 0;
					font-size: 24rpx;
					color: #FFFFFF;
					line-height: 34rpx;
				}
			}
			&__allbtn{
				font-size: 24rpx;
				color: #595A66;
				background: #ededed;
				border-radius: 10rpx;
				padding: 10rpx 15rpx;
				margin-top: 40rpx;
			}
		}
	}
</style>